import { logout } from "@/store/actions/login"
import { isAuth } from "@/utils/token"
import { useDispatch } from "react-redux"
import { Route, Redirect, RouteProps, useLocation } from "react-router-dom"
const AuthRoute = ({ children, ...rest }: RouteProps) => {
  const location = useLocation()
  const dispatch = useDispatch()
  return (
    <Route
      {...rest}
      render={() => {
        const isLogin = isAuth()
        if (isLogin) {
          // 登录
          return children
        }
        dispatch(logout())
        // 未登录
        return (
          <Redirect
            to={{
              pathname: "/login",
              state: {
                from: location.pathname,
              },
            }}
          />
        )
      }}
    ></Route>
  )
}
export default AuthRoute
